package org.lanqiao.music.system.service.impl;

import org.lanqiao.music.common.dto.MusicDTO;
import org.lanqiao.music.system.entity.Music;
import org.lanqiao.music.system.dao.MusicDao;
import org.lanqiao.music.system.service.MusicService;
import org.lanqiao.music.system.vo.HotMusicVo;
import org.lanqiao.music.system.vo.MusicVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import org.lanqiao.music.common.utils.PageDTO;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * (Music)表服务实现类
 *
 * @author makejava
 * @since 2024-08-15 20:50:28
 */
@Service
public class MusicServiceImpl implements MusicService {
    @Autowired
    private MusicDao musicDao;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    public MusicVO queryById(Integer musicId) {
        return musicDao.queryById(musicId);
    }

    /**
     * 分页查询
     *
     * @param music 筛选条件
     * @return 查询结果
     */
    @Override
    public PageInfo<Music> queryByPage(Music music, PageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPageNum(),pageDTO.getPageSize());
        PageInfo page = new PageInfo<>(this.musicDao.queryAllByLimit(music));
        return page;
    }

    /**
     * 新增数据
     *
     * @param record 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(Music record) {
        return this.musicDao.insert(record) ;
    }

    /**
     * 批量新增音乐
     * @param entities List<Music> 实例对象列表
     * @return
     */
    @Override
    public int insertBatch(List<Music> entities) {
        return musicDao.insertBatch(entities);
    }

    /**
     * 修改数据
     *
     * @param music 实例对象
     * @return 实例对象
     */
    @Override
    public int update(Music music) {
        return musicDao.update(music);
    }

    /**
     * 通过主键删除数据
     *
     * @param musicId 主键
     * @return 是否成功
     */
    @Override
    public int deleteById(Integer musicId) {
        return this.musicDao.deleteById(musicId);
    }

    //批量删除
    @Override
    public int deleteBatch(List<Long> musicIds) {
        return musicDao.deleteBatch(musicIds);
    }

    /**
     *
     * @param dto
     * 通过歌手或歌曲查找
     * @param
     * @return
     */
    @Override
//    @Cacheable(keyGenerator = "myKeyGenerator")
    public PageInfo<MusicVO> searchMusic(MusicDTO dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        PageInfo page = new PageInfo<>(this.musicDao.searchMusic(dto));
        return page;
    }

    /**
     * 通过歌手或歌曲查找
     * @param
     * @return
     */

    @Override
    public String getLyricsByMusicId(Integer musicId) {
        return musicDao.getLyricsByMusicId(musicId);
    }

    @Override
    public String getMusicFilePathByMusicId(Integer musicId) {
        return musicDao.getMusicFilePathByMusicId(musicId);
    }

    @Override
    public List<HotMusicVo> getHotMusic() {
        System.out.println("热点数据get service");
        Set<Object> hotMusic = redisTemplate.opsForZSet().reverseRangeByScore("hotMusic", 1, 10);
        Integer rank = 1;
        List<HotMusicVo> hotMusicVos = new ArrayList<>();
        for (Object o : hotMusic){
            System.out.println(o);
            HotMusicVo hotMusicVo = new HotMusicVo();
            MusicVO music = musicDao.queryById((Integer) o);
            System.out.println(music.getMusicName());
            hotMusicVo.setMusicId((Integer) o);
            hotMusicVo.setMusicName(music.getMusicName());
            hotMusicVo.setRank(rank++);
            hotMusicVos.add(hotMusicVo);
        }
        return hotMusicVos;
    }

    @Override
    public void hotMusicAdd(Integer musicId) {
        System.out.println("热点数据service");
        /*System.out.println(redisTemplate.opsForZSet().incrementScore("hotMusic",musicId,1));*/
        if (redisTemplate.opsForZSet().rank("hotMusic",musicId) == null){
            redisTemplate.opsForZSet().add("hotMusic",musicId,1);
        }else {
            redisTemplate.opsForZSet().incrementScore("hotMusic", musicId, 1);
        }


    }
}
